import { SEARCH_BLUR, SEARCH_FOCUS, CHANGE_LIST, MOUSE_ENTER, MOUSE_LEAVE, CHANGE_PAGE} from "../actionTypes";
import { fromJS } from "immutable";

const defaultState = fromJS({
    focused: false,
    list: [],
    totalPage: 1,
    page: 1,
    mouseEnter: false
})
// 不使用immutable的方式

export default(state=defaultState, action) => {
    switch(action.type){
        case SEARCH_FOCUS:
            // immutable对象的set方法，会结合之前immutable的值和设置的值，返回一个全新的对象
            return state.set('focused', true)
        case SEARCH_BLUR:
            return state.set('focused', false)
        case CHANGE_LIST:
            return state.set('list', action.data).set('totalPage', action.totalPage)
        case MOUSE_ENTER:
            return state.set('mouseEnter', true)
        case MOUSE_LEAVE:
                return state.set('mouseEnter', false)
        case CHANGE_PAGE:
            return state.set('page', action.page)
        default: 
            return state
    }
}
// 不使用immutable的方式
// export default(state=defaultState, action) => {
//     if(action.type===SEARCH_FOCUS){
//         return {focused: true}
//     }
//     if(action.type===SEARCH_BLUR){
//         return {focused: false}
//     }
//     return state
// }